#include "translator.h"
#include "../SceneData/AnimatePropertyNode.h"
#include <QFile>
#include <QTextStream>
#include <QDir>
Translator::Translator()
	:script_preload(),script_lib(),manifest_pos(0),
	script_loaded(),symbol_id(0),shape_id(0),project(),script_output(),res_ref(),pic_id(0),mclip_id(0),container_id(0),width(0),height(0),bk(255,255,255,255)//,res_id()
{
	//Initialize();
}

void Translator::Clear()
{
	//root=0;
	script_preload.clear();
	script_loaded.clear();
	script_lib.clear();
	manifest_pos=0;
	//fps_pos=0;
	//fps_len=0;
	symbol_id=0;
	shape_id=0;
	pic_id=0;
	mclip_id=0;
	container_id=0;
	width=0;
	height=0;
	bk=QColor(255,255,255,255);
	project.clear();
	script_output.clear();
	//res_id.clear();
	res_ref.clear();
}
void Translator::Initialize(SceneNode *scene)
{
	project=scene->name;
	width=scene->Width;
	height=scene->Height;
	bk=scene->BackgroundColor;
	script_preload+="var canvas, stage, exportRoot, images;\r\n";
	script_preload+="function init() {\r\n";
	script_preload+="canvas = document.getElementById(\"canvas\");\r\n";
	script_preload+="images = {};\r\n";
	script_preload+="var manifest = [\r\n";
	manifest_pos=script_preload.size();
	//{src:\"images/_3c2c4bfb068a2e776c22eb70.jpg\", id:\"_3c2c4bfb068a2e776c22eb70\"}
	script_preload+="];\r\n";
	script_preload+="var loader = new PreloadJS(false);\r\n";
	script_preload+="loader.onFileLoad = handleFileLoad;\r\n";
	script_preload+="loader.onComplete = handleComplete;\r\n";
	script_preload+="loader.loadManifest(manifest);\r\n";
	script_preload+="}\r\n";
	script_preload+="function handleFileLoad(o) {\r\n";
	script_preload+="if (o.type == \"image\") { images[o.id] = o.result; }\r\n";
	script_preload+="}\r\n";
	script_loaded+="function handleComplete() {\r\n";
	script_loaded+="exportRoot = new lib."+project;
	script_loaded+="();\r\n";
	script_loaded+="stage = new Stage(canvas);\r\n";
	script_loaded+="stage.addChild(exportRoot);\r\n";
	script_loaded+="stage.update();\r\n";
	script_loaded+="Ticker.setFPS(";
	script_loaded+=QString::number(scene->FPS);
	script_loaded+=");\r\n";
	script_loaded+="Ticker.addListener(stage);\r\n";
	script_loaded+="}";
	script_lib.append(QString());
	script_lib[0]+="if (!window.lib) { window.lib = {}; }\r\n var p; \r\n";
}

void Translator::parseRootNode(ContainerNode *root)
{
	QString obj;
	obj+="(lib.";
	obj+=project;
	obj+=" = function(mode,startPosition,loop) {\r\nthis.initialize(mode,startPosition,loop,{},true);\r\n";
	script_lib[0].append(obj);
	for(int i=root->children.size()-1;i!=-1;--i)
	{
		switch((root->children)[i]->NodeType())
		{
		case MovieClip:
			parseMovieClipNode((MovieClipNode *)((root->children)[i]),0);
			break;
		case Symbol:
			parseSymbolNode((SymbolNode *)((root->children)[i]),0);
			break;
		}
	}
	obj="";
	obj+="}).prototype = p = new MovieClip();\r\n";
	script_lib[0].append(obj);
	script_output+=script_preload+script_loaded;
	for(int i=0;i!=script_lib.size();++i)
		script_output+=script_lib[i];
}

void Translator::parsePathNode(PathNode * pt,int pos)
{
	
	//static unsigned int shape_id=0;
	if(!(pt->points.size()))
		return;
	QString temp;
	
    temp+="this.shape"+QString::number(shape_id)+" = new Shape();\r\n";
    QPointF pf;double pr;const QGradient *g_pt;
	switch(pt->FillBrush.style())
	{
	case Qt::SolidPattern:
		temp+="this.shape"+QString::number(shape_id)+".graphics.beginFill(\"rgba(";
		temp+=QString::number( pt->FillBrush.color().red());
		temp+=",";
		temp+=QString::number( pt->FillBrush.color().green());
		temp+=",";
		temp+=QString::number( pt->FillBrush.color().blue());
		temp+=",";
		temp+=QString::number( pt->FillBrush.color().alpha()/255.0);
		temp+=")\")";
		break;
	case Qt::LinearGradientPattern:
		temp+="this.shape"+QString::number(shape_id)+".graphics.beginLinearGradientFill([";
        g_pt= pt->FillBrush.gradient();
		for(int i=0;i!=g_pt->stops().size();++i)
		{
			QColor c=(g_pt->stops())[i].second;
			temp+="\"rgba("+QString::number( c.red()) 
				+","+QString::number( c.green()) 
				+","+QString::number( c.blue()) 
				+","+QString::number( c.alpha()/255.0) +")\",";
		}
		temp+="],[";
		for(int i=0;i!=g_pt->stops().size();++i)
		{
			double d=(g_pt->stops())[i].first;
			temp+= QString::number( d) +",";
		}
		temp+="],";
        pf=((QLinearGradient*)g_pt)->start();
		temp+=QString::number(pf.x())+","+QString::number(pf.y())+",";
		pf=((QLinearGradient*)g_pt)->finalStop();
		temp+=QString::number(pf.x())+","+QString::number(pf.y())+")";
		break;
	case Qt::RadialGradientPattern:
		temp+="this.shape"+QString::number(shape_id)+".graphics.beginRadialGradientFill([";
        g_pt= pt->FillBrush.gradient();
        for(int i=0;i!=g_pt->stops().size();++i)
		{
            QColor c=(g_pt->stops())[i].second;
			temp+="\"rgba("+QString::number( c.red()) 
				+","+QString::number( c.green()) 
				+","+QString::number( c.blue()) 
				+","+QString::number( c.alpha()/255.0) +")\",";
		}
		temp+="],[";
        for(int i=0;i!=g_pt->stops().size();++i)
		{
            double d=(g_pt->stops())[i].first;
			temp+= QString::number( d) +",";
		}
		temp+="],";
        pf=((QRadialGradient*)g_pt)->focalPoint();
        pr=((QRadialGradient*)g_pt)->focalRadius();
        temp+=QString::number(pf.x())+","+QString::number(pf.y())+","+QString::number(pr)+",";
        pf=((QRadialGradient*)g_pt)->center();
        pr=((QRadialGradient*)g_pt)->centerRadius();
        temp+=QString::number(pf.x())+","+QString::number(pf.y())+","+QString::number(pr)+")";
		break;
	}
	int i=0;
	QString temp1;
	while(i!=pt->path.elementCount())
	{
		QPainterPath::Element e= pt->path.elementAt(i);
		switch(e.type)
		{
		case QPainterPath::MoveToElement:
			temp1+=".moveTo("+QString::number(e.x)+","+QString::number(e.y)+")";
			++i;
			break;
		case QPainterPath::LineToElement:
			temp1+=".lineTo("+QString::number(e.x)+","+QString::number(e.y)+")";
			++i;
			break;
		case QPainterPath::CurveToElement:
			temp1+=".bezierCurveTo("+QString::number(e.x)+","+QString::number(e.y)+",";
			++i;
			e= pt->path.elementAt(i);
			temp1+=QString::number(e.x)+","+QString::number(e.y)+",";
			++i;
			e= pt->path.elementAt(i);
			temp1+=QString::number(e.x)+","+QString::number(e.y)+")";
			++i;
			break;
		}
	}
	temp+=temp1+".endFill().setStrokeStyle("
		+QString::number( pt->LinePen.width())
		+",";
	switch(pt->LinePen.capStyle())
	{
	case Qt::FlatCap:
		temp+="0";
		break;
	case Qt::RoundCap:
		temp+="1";
		break;
	case Qt::SquareCap:
		temp+="2";
		break;
	}
	temp+=",";
	switch(pt->LinePen.joinStyle())
	{
	case Qt::MiterJoin:
		temp+="0";
		break;
	case Qt::RoundJoin:
		temp+="1";
		break;
	case Qt::BevelJoin:
		temp+="2";
		break;
	}
	temp+=","+QString::number( pt->LinePen.miterLimit())+").beginStroke(\"rgba(";
	temp+=QString::number( pt->LinePen.color().red());
	temp+=",";
	temp+=QString::number( pt->LinePen.color().green());
	temp+=",";
	temp+=QString::number( pt->LinePen.color().blue());
	temp+=",";
	temp+=QString::number( pt->LinePen.color().alpha()/255.0);
	temp+=")\")";
	temp+=temp1;
	if(pt->isClosed)
		temp+=".closePath()";
	temp+=".endStroke();";
	temp+="this.shape";
	temp+=QString::number(shape_id)+".setTransform("
		+QString::number(pt->PosX->ValueOnCurrentTime(0))
		+","+QString::number(pt->PosY->ValueOnCurrentTime(0))
		+","+QString::number(pt->ScaleX->ValueOnCurrentTime(0))
		+","+QString::number(pt->ScaleY->ValueOnCurrentTime(0))
		+","+QString::number(pt->RotationAngle->ValueOnCurrentTime(0))
		+","+QString::number(pt->SkewX->ValueOnCurrentTime(0))
		+","+QString::number(pt->SkewY->ValueOnCurrentTime(0))
		+","+QString::number(pt->AxisX->ValueOnCurrentTime(0))
		+","+QString::number(pt->AxisY->ValueOnCurrentTime(0))
		+");this.addChild(this.shape"+QString::number(shape_id)+");";
	script_lib[pos].append(temp);
	++shape_id;
}

void Translator::parsePictureNode(PictureNode *pt,int pos)
{
	QString image;
	//static int pic_id=0;
	image+="{src:\"";
	image+=QString::number(pic_id,36);//pt->GetPath();
	image+=".png\",id:\"_";
	image+=QString::number(pic_id,36)+"\"},";
	script_preload.insert(manifest_pos,image);
	manifest_pos+=image.size();
	res_ref.push_back(QImage(pt->GetImage()));
	QString obj;
	obj+="(lib.pic";
	obj+=QString::number(pic_id,36)+" = function() {\r\n this.initialize(images._";
	obj+=QString::number(pic_id,36)+");\r\n}).prototype = new Bitmap();\r\n";
	script_lib.append(obj);
	QString temp;
	temp+="this.instance";
	temp+=QString::number(symbol_id,36);
	temp+=" = new lib.pic";
	temp+=QString::number(pic_id,36);
	temp+="();\r\n";

	
	if((pt->Alpha->ValueOnCurrentTime(0))!=1)
	{
		temp+="this.instance";
		temp+=QString::number(symbol_id,36);
		temp+=".alpha=";
		temp+=QString::number(pt->Alpha->ValueOnCurrentTime(0))+";";
	}


	temp+="this.instance";
	temp+=QString::number(symbol_id,36);
	temp+=".setTransform(";
	temp+=QString::number(pt->PosX->ValueOnCurrentTime(0))
		+","+QString::number(pt->PosY->ValueOnCurrentTime(0))
		+","+QString::number(pt->ScaleX->ValueOnCurrentTime(0))
		+","+QString::number(pt->ScaleY->ValueOnCurrentTime(0))
		+","+QString::number(pt->RotationAngle->ValueOnCurrentTime(0))
		+","+QString::number(pt->SkewX->ValueOnCurrentTime(0))
		+","+QString::number(pt->SkewY->ValueOnCurrentTime(0))
		+","+QString::number(pt->AxisX->ValueOnCurrentTime(0))
		+","+QString::number(pt->AxisY->ValueOnCurrentTime(0))
		+");this.addChild(this.instance"+QString::number(symbol_id,36)+");";
	script_lib[pos].append(temp);
	++pic_id;
	++symbol_id;
}

void Translator::parseSymbolNode(SymbolNode *pt,int pos)
{
	QString obj,temp;
	//static int container_id=0;
	temp+="this.instance";
	temp+=QString::number(symbol_id,36);
	temp+=" = new lib.container";
	temp+=QString::number(container_id,36);
	temp+="();\r\nthis.instance";
	temp+=QString::number(symbol_id,36);
	temp+=".setTransform(";
	temp+=QString::number(pt->PosX->ValueOnCurrentTime(0))
		+","+QString::number(pt->PosY->ValueOnCurrentTime(0))
		+","+QString::number(pt->ScaleX->ValueOnCurrentTime(0))
		+","+QString::number(pt->ScaleY->ValueOnCurrentTime(0))
		+","+QString::number(pt->RotationAngle->ValueOnCurrentTime(0))
		+","+QString::number(pt->SkewX->ValueOnCurrentTime(0))
		+","+QString::number(pt->SkewY->ValueOnCurrentTime(0))
		+","+QString::number(pt->AxisX->ValueOnCurrentTime(0))
		+","+QString::number(pt->AxisY->ValueOnCurrentTime(0))
		+","+QString::number(pt->AxisY->ValueOnCurrentTime(0))
		+");\r\n";
	if(!(pt->Visible->ValueOnCurrentTime(0)))
	{
		temp+="this.instance";
		temp+=QString::number(symbol_id,36);
		temp+="._off=true;";
	}
	if((pt->Alpha->ValueOnCurrentTime(0))!=1)
	{
		temp+="this.instance";
		temp+=QString::number(symbol_id,36);
		temp+=".alpha=";
		temp+=QString::number(pt->Alpha->ValueOnCurrentTime(0))+";";
	}
	QVector<int> times=pt->getKeyFrames(VISIBLE);
	//if(times.size()>1)
	//{
		temp+="this.timeline.addTween(Tween.get(this.instance";
		temp+=QString::number(symbol_id,36)+")";
		for(int i=1;i<times.size();++i)
		{
			temp+=".to({_off:";
			temp+=((pt->Visible->ValueOnCurrentTime(times[i]))==1)?"false":"true";
			temp+="},";
			temp+=QString::number(times[i]-times[i-1]);
			temp+=")";
		}
		temp+=");\r\n";
	//}
	parseAnimation(POSX,pt,"x",temp);
	parseAnimation(POSY,pt,"y",temp);
	parseAnimation(AXISX,pt,"regX",temp);
	parseAnimation(AXISY,pt,"regY",temp);
	parseAnimation(SCALEX,pt,"scaleX",temp);
	parseAnimation(SCALEY,pt,"scaleY",temp);
	parseAnimation(SKEWX,pt,"skewX",temp);
	parseAnimation(SKEWY,pt,"skewY",temp);
	parseAnimation(ANGLE,pt,"rotation",temp);
	parseAnimation(ALPHA,pt,"alpha",temp);
	script_lib[pos].append(temp);
	++symbol_id;
	int pos_obj=script_lib.size();
	obj+="(lib.container";
	obj+=QString::number(container_id,36);
	obj+=" = function() {\r\nthis.initialize();\r\n";
	script_lib.append(obj);
	++container_id;
	for(int i=0;i!=pt->children.size();++i)
	{
		switch((pt->children)[i]->NodeType())
		{
		case Path:
			parsePathNode((PathNode *)((pt->children)[i]),pos_obj);
			break;
		case Picture:
			parsePictureNode((PictureNode *)((pt->children)[i]),pos_obj);
			break;
		case Rect:
			parseRectNode((RectNode *)((pt->children)[i]),pos_obj);
			break;
		
		case Eclipse:
			parseEclipseNode((EclipseNode *)((pt->children)[i]),pos_obj);
			break;

		case WordShape:
			PathNode * pth=new PathNode(  (WordShapeNode *)((pt->children)[i]) );
			parsePathNode(pth,pos_obj);
			delete pth;
			break;

		}
	}
	obj="";
	obj+="}).prototype = p = new Container();\r\n";
	script_lib[pos_obj].append(obj);
}

void Translator::parseMovieClipNode(MovieClipNode *pt,int pos)
{
	QString obj,temp;
	//static int mclip_id=0;
	temp+="this.instance";
	temp+=QString::number(symbol_id,36);
	temp+=" = new lib.movieclip";
	temp+=QString::number(mclip_id,36);
	temp+="();\r\nthis.instance";
	temp+=QString::number(symbol_id,36);
	temp+=".setTransform(";
	temp+=QString::number(pt->PosX->ValueOnCurrentTime(0))
		+","+QString::number(pt->PosY->ValueOnCurrentTime(0))
		+","+QString::number(pt->ScaleX->ValueOnCurrentTime(0))
		+","+QString::number(pt->ScaleY->ValueOnCurrentTime(0))
		+","+QString::number(pt->RotationAngle->ValueOnCurrentTime(0))
		+","+QString::number(pt->SkewX->ValueOnCurrentTime(0))
		+","+QString::number(pt->SkewY->ValueOnCurrentTime(0))
		+","+QString::number(pt->AxisX->ValueOnCurrentTime(0))
		+","+QString::number(pt->AxisY->ValueOnCurrentTime(0))
		+");\r\n";
	if(!(pt->Visible->ValueOnCurrentTime(0)))
	{
		temp+="this.instance";
		temp+=QString::number(symbol_id,36);
		temp+="._off=true;";
	}
	if((pt->Alpha->ValueOnCurrentTime(0))!=1)
	{
		temp+="this.instance";
		temp+=QString::number(symbol_id,36);
		temp+=".alpha=";
		temp+=QString::number(pt->Alpha->ValueOnCurrentTime(0))+";";
	}
	QVector<int> times=pt->getKeyFrames(VISIBLE);
	//if(times.size()>1)
	//{
		temp+="this.timeline.addTween(Tween.get(this.instance";
		temp+=QString::number(symbol_id,36)+")";
		for(int i=1;i<times.size();++i)
		{
			temp+=".to({_off:";
			temp+=((pt->Visible->ValueOnCurrentTime(times[i]))==1)?"false":"true";
			temp+="},";
			temp+=QString::number(times[i]-times[i-1]);
			temp+=")";
		}
		temp+=");\r\n";
	//}
	parseAnimation(POSX,pt,"x",temp);
	parseAnimation(POSY,pt,"y",temp);
	parseAnimation(AXISX,pt,"regX",temp);
	parseAnimation(AXISY,pt,"regY",temp);
	parseAnimation(SCALEX,pt,"scaleX",temp);
	parseAnimation(SCALEY,pt,"scaleY",temp);
	parseAnimation(SKEWX,pt,"skewX",temp);
	parseAnimation(SKEWY,pt,"skewY",temp);
	parseAnimation(ANGLE,pt,"rotation",temp);
	parseAnimation(ALPHA,pt,"alpha",temp);
	script_lib[pos].append(temp);
	++symbol_id;
	int pos_obj=script_lib.size();
	obj+="(lib.movieclip";
	obj+=QString::number(mclip_id,36);
	obj+=" = function() {\r\nthis.initialize();\r\n";
	script_lib.append(obj);
	++mclip_id;
	for(int i=pt->children.size()-1;i!=-1;--i)
	{
		switch((pt->children)[i]->NodeType())
		{
		case MovieClip:
			parseMovieClipNode((MovieClipNode *)((pt->children)[i]),pos_obj);
			break;
		case Symbol:
			parseSymbolNode((SymbolNode *)((pt->children)[i]),pos_obj);
			break;

		}
	}
	obj="";
	obj+="}).prototype = p = new MovieClip();\r\n";
	script_lib[pos_obj].append(obj);
}



void Translator::parseRectNode(RectNode * pt,int pos)
{	
	QString temp;
	
    temp+="this.shape"+QString::number(shape_id)+" = new Shape();\r\n";
    QPointF pf;double pr;const QGradient *g_pt;
	switch(pt->FillBrushRef().style())
	{
	case Qt::SolidPattern:
		temp+="this.shape"+QString::number(shape_id)+".graphics.beginFill(\"rgba(";
		temp+=QString::number( pt->FillBrushRef().color().red());
		temp+=",";
		temp+=QString::number( pt->FillBrushRef().color().green());
		temp+=",";
		temp+=QString::number( pt->FillBrushRef().color().blue());
		temp+=",";
		temp+=QString::number( pt->FillBrushRef().color().alpha()/255.0);
		temp+=")\")";
		break;
	case Qt::LinearGradientPattern:
		temp+="this.shape"+QString::number(shape_id)+".graphics.beginLinearGradientFill([";
        g_pt= pt->FillBrushRef().gradient();
		for(int i=0;i!=g_pt->stops().size();++i)
		{
			QColor c=(g_pt->stops())[i].second;
			temp+="\"rgba("+QString::number( c.red()) 
				+","+QString::number( c.green()) 
				+","+QString::number( c.blue()) 
				+","+QString::number( c.alpha()/255.0) +")\",";
		}
		temp+="],[";
		for(int i=0;i!=g_pt->stops().size();++i)
		{
			double d=(g_pt->stops())[i].first;
			temp+= QString::number( d) +",";
		}
		temp+="],";
        pf=((QLinearGradient*)g_pt)->start();
		temp+=QString::number(pf.x())+","+QString::number(pf.y())+",";
		pf=((QLinearGradient*)g_pt)->finalStop();
		temp+=QString::number(pf.x())+","+QString::number(pf.y())+")";
		break;
	case Qt::RadialGradientPattern:
		temp+="this.shape"+QString::number(shape_id)+".graphics.beginRadialGradientFill([";
        g_pt= pt->FillBrushRef().gradient();
        for(int i=0;i!=g_pt->stops().size();++i)
		{
            QColor c=(g_pt->stops())[i].second;
			temp+="\"rgba("+QString::number( c.red()) 
				+","+QString::number( c.green()) 
				+","+QString::number( c.blue()) 
				+","+QString::number( c.alpha()/255.0) +")\",";
		}
		temp+="],[";
        for(int i=0;i!=g_pt->stops().size();++i)
		{
            double d=(g_pt->stops())[i].first;
			temp+= QString::number( d) +",";
		}
		temp+="],";
        pf=((QRadialGradient*)g_pt)->focalPoint();
        pr=((QRadialGradient*)g_pt)->focalRadius();
        temp+=QString::number(pf.x())+","+QString::number(pf.y())+","+QString::number(pr)+",";
        pf=((QRadialGradient*)g_pt)->center();
        pr=((QRadialGradient*)g_pt)->centerRadius();
        temp+=QString::number(pf.x())+","+QString::number(pf.y())+","+QString::number(pr)+")";
		break;
	}
	
	temp+=".setStrokeStyle("
		+QString::number( pt->LinePen.width())
		+",";
	switch(pt->LinePenRef().capStyle())
	{
	case Qt::FlatCap:
		temp+="0";
		break;
	case Qt::RoundCap:
		temp+="1";
		break;
	case Qt::SquareCap:
		temp+="2";
		break;
	}
	temp+=",";
	switch(pt->LinePenRef().joinStyle())
	{
	case Qt::MiterJoin:
		temp+="0";
		break;
	case Qt::RoundJoin:
		temp+="1";
		break;
	case Qt::BevelJoin:
		temp+="2";
		break;
	}
	temp+=","+QString::number( pt->LinePenRef().miterLimit())+").beginStroke(\"rgba(";
	temp+=QString::number( pt->LinePenRef().color().red());
	temp+=",";
	temp+=QString::number( pt->LinePenRef().color().green());
	temp+=",";
	temp+=QString::number( pt->LinePenRef().color().blue());
	temp+=",";
	temp+=QString::number( pt->LinePenRef().color().alpha()/255.0);
	temp+=")\")";
	temp+=".rect(0,0,";
	temp+=QString::number(pt->GetRectWidth())+","+QString::number(pt->GetRectHeight())+")";

	
	temp+=".endStroke();";
	temp+="this.shape";
	temp+=QString::number(shape_id)+".setTransform("
		+QString::number(pt->GetPosX())
		+","+QString::number(pt->GetPosY())
		+","+QString::number(pt->GetScaleX())
		+","+QString::number(pt->GetScaleY())
		+","+QString::number(pt->GetAngle())
		+","+QString::number(pt->GetSkewX())
		+","+QString::number(pt->GetSkewY())
		+","+QString::number(pt->GetAxisX())
		+","+QString::number(pt->GetAxisY())
		+");this.addChild(this.shape"+QString::number(shape_id)+");";
	script_lib[pos].append(temp);
	++shape_id;
}


void Translator::parseEclipseNode(EclipseNode *pt,int pos)
{	
	QString temp;
	
    temp+="this.shape"+QString::number(shape_id)+" = new Shape();\r\n";
    QPointF pf;double pr;const QGradient *g_pt;
	switch(pt->FillBrushRef().style())
	{
	case Qt::SolidPattern:
		temp+="this.shape"+QString::number(shape_id)+".graphics.beginFill(\"rgba(";
		temp+=QString::number( pt->FillBrushRef().color().red());
		temp+=",";
		temp+=QString::number( pt->FillBrushRef().color().green());
		temp+=",";
		temp+=QString::number( pt->FillBrushRef().color().blue());
		temp+=",";
		temp+=QString::number( pt->FillBrushRef().color().alpha()/255.0);
		temp+=")\")";
		break;
	case Qt::LinearGradientPattern:
		temp+="this.shape"+QString::number(shape_id)+".graphics.beginLinearGradientFill([";
        g_pt= pt->FillBrushRef().gradient();
		for(int i=0;i!=g_pt->stops().size();++i)
		{
			QColor c=(g_pt->stops())[i].second;
			temp+="\"rgba("+QString::number( c.red()) 
				+","+QString::number( c.green()) 
				+","+QString::number( c.blue()) 
				+","+QString::number( c.alpha()/255.0) +")\",";
		}
		temp+="],[";
		for(int i=0;i!=g_pt->stops().size();++i)
		{
			double d=(g_pt->stops())[i].first;
			temp+= QString::number( d) +",";
		}
		temp+="],";
        pf=((QLinearGradient*)g_pt)->start();
		temp+=QString::number(pf.x())+","+QString::number(pf.y())+",";
		pf=((QLinearGradient*)g_pt)->finalStop();
		temp+=QString::number(pf.x())+","+QString::number(pf.y())+")";
		break;
	case Qt::RadialGradientPattern:
		temp+="this.shape"+QString::number(shape_id)+".graphics.beginRadialGradientFill([";
        g_pt= pt->FillBrushRef().gradient();
        for(int i=0;i!=g_pt->stops().size();++i)
		{
            QColor c=(g_pt->stops())[i].second;
			temp+="\"rgba("+QString::number( c.red()) 
				+","+QString::number( c.green()) 
				+","+QString::number( c.blue()) 
				+","+QString::number( c.alpha()/255.0) +")\",";
		}
		temp+="],[";
        for(int i=0;i!=g_pt->stops().size();++i)
		{
            double d=(g_pt->stops())[i].first;
			temp+= QString::number( d) +",";
		}
		temp+="],";
        pf=((QRadialGradient*)g_pt)->focalPoint();
        pr=((QRadialGradient*)g_pt)->focalRadius();
        temp+=QString::number(pf.x())+","+QString::number(pf.y())+","+QString::number(pr)+",";
        pf=((QRadialGradient*)g_pt)->center();
        pr=((QRadialGradient*)g_pt)->centerRadius();
        temp+=QString::number(pf.x())+","+QString::number(pf.y())+","+QString::number(pr)+")";
		break;
	}
	
	temp+=".setStrokeStyle("
		+QString::number( pt->LinePenRef().width())
		+",";
	switch(pt->LinePenRef().capStyle())
	{
	case Qt::FlatCap:
		temp+="0";
		break;
	case Qt::RoundCap:
		temp+="1";
		break;
	case Qt::SquareCap:
		temp+="2";
		break;
	}
	temp+=",";
	switch(pt->LinePenRef().joinStyle())
	{
	case Qt::MiterJoin:
		temp+="0";
		break;
	case Qt::RoundJoin:
		temp+="1";
		break;
	case Qt::BevelJoin:
		temp+="2";
		break;
	}
	temp+=","+QString::number( pt->LinePenRef().miterLimit())+").beginStroke(\"rgba(";
	temp+=QString::number( pt->LinePenRef().color().red());
	temp+=",";
	temp+=QString::number( pt->LinePenRef().color().green());
	temp+=",";
	temp+=QString::number( pt->LinePenRef().color().blue());
	temp+=",";
	temp+=QString::number( pt->LinePenRef().color().alpha()/255.0);
	temp+=")\")";
	temp+=".drawEllipse(0,0,";
	temp+=QString::number(pt->GetRectWidth())+","+QString::number(pt->GetRectHeight())+")";

	
	temp+=".endStroke();";
	temp+="this.shape";
	temp+=QString::number(shape_id)+".setTransform("
		+QString::number(pt->GetPosX())
		+","+QString::number(pt->GetPosY())
		+","+QString::number(pt->GetScaleX())
		+","+QString::number(pt->GetScaleY())
		+","+QString::number(pt->GetAngle())
		+","+QString::number(pt->GetSkewX())
		+","+QString::number(pt->GetSkewY())
		+","+QString::number(pt->GetAxisX())
		+","+QString::number(pt->GetAxisY())
		+");this.addChild(this.shape"+QString::number(shape_id)+");";
	script_lib[pos].append(temp);
	++shape_id;
}


bool Translator::Output(const QString & path)
{
	
    QDir dir;// = new QDir;
	dir.mkpath(path);
	dir.mkpath(path+"libs\\");
	QFile::copy("libs\\easeljs-0.4.2.min.js",path+"libs\\easeljs-0.4.2.min.js");
	QFile::copy("libs\\movieclip-0.4.1.min.js",path+"libs\\movieclip-0.4.1.min.js");
	QFile::copy("libs\\preloadjs-0.1.0.min.js",path+"libs\\preloadjs-0.1.0.min.js");
	QFile::copy("libs\\tweenjs-0.2.0.min.js",path+"libs\\tweenjs-0.2.0.min.js");
	QFile file( path+project+"-code.js" );
	for(int i=0;i!=res_ref.size();++i)
		res_ref[i].save(path+QString::number(i,36)+".png","PNG");
    if ( file.open(QIODevice::WriteOnly ) )
    {
        QTextStream stream( &file );
        stream <<script_output;
        file.close();
		//return true;
    }
	else
		return false;
	QString html;
	html+="<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"UTF-8\">\n<title>CreateJS export </title>\n<script src=\"libs/easeljs-0.4.2.min.js\"></script>\n";
	html+="<script src=\"libs/tweenjs-0.2.0.min.js\"></script>\n<script src=\"libs/movieclip-0.4.1.min.js\"></script>\n";
	html+="<script src=\"libs/preloadjs-0.1.0.min.js\"></script>\n<script src=\"";
	html+=project+"-code.js\"></script>\n";
	html+="</head>\n<body onload=\"init();\" style=\"background-color:#D4D4D4\">";
	html+="<canvas id=\"canvas\" width=\"";
	html+=QString::number(width);
	html+="\" height=\"";
	html+=QString::number(height);
	html+="\" style=\"background-color:rgba(";
	html+=QString::number( bk.red());
	html+=",";
	html+=QString::number( bk.green());
	html+=",";
	html+=QString::number( bk.blue());
	html+=",";
	html+=QString::number( bk.alpha()/255.0);
	html+=")\"></canvas>\n</body>\n</html>";
	QFile file1( path+project+".html" );
	if ( file1.open(QIODevice::WriteOnly ) )
    {
        QTextStream stream( &file1 );
        stream <<html;
        file1.close();
		//return true;
    }
	else
		return false;
	return true;
}


void Translator::parseAnimation(int flag,ContainerNode* pt,QString Property,QString& out)
{
	//QVector<int> times;
	int n;
	AnimatePropertyNode * ap;
	switch(flag)
	{
	case POSX:
		n=((AnimatePropertyNode*)(pt->PosX))->KeyValues.size();
		ap=((AnimatePropertyNode*)(pt->PosX));
		break;
	case POSY:
		n=((AnimatePropertyNode*)(pt->PosY))->KeyValues.size();
		ap=((AnimatePropertyNode*)(pt->PosY));
		break;
	case AXISX:
		n=((AnimatePropertyNode*)(pt->AxisX))->KeyValues.size();
		ap=((AnimatePropertyNode*)(pt->AxisX));
		break;
	case AXISY:
		n=((AnimatePropertyNode*)(pt->AxisY))->KeyValues.size();
		ap=((AnimatePropertyNode*)(pt->AxisY));
		break;
	case SCALEX:
		n=((AnimatePropertyNode*)(pt->ScaleX))->KeyValues.size();
		ap=((AnimatePropertyNode*)(pt->ScaleX));
		break;
	case SCALEY:
		n=((AnimatePropertyNode*)(pt->ScaleY))->KeyValues.size();
		ap=((AnimatePropertyNode*)(pt->ScaleY));
		break;
	case SKEWX:
		n=((AnimatePropertyNode*)(pt->SkewX))->KeyValues.size();
		ap=((AnimatePropertyNode*)(pt->SkewX));
		break;
	case SKEWY:
		n=((AnimatePropertyNode*)(pt->SkewY))->KeyValues.size();
		ap=((AnimatePropertyNode*)(pt->SkewY));
		break;
	case ANGLE:
		n=((AnimatePropertyNode*)(pt->RotationAngle))->KeyValues.size();
		ap=((AnimatePropertyNode*)(pt->RotationAngle));
		break;
	case ALPHA:
		n=((AnimatePropertyNode*)(pt->Alpha))->KeyValues.size();
		ap=((AnimatePropertyNode*)(pt->Alpha));
		break;
	}
	if(n<=1)
		return;
	out+="this.timeline.addTween(Tween.get(this.instance";
	out+=QString::number(symbol_id,36)+")";
	for(int i=1;i<n;++i)
	{
		out+=".to({";
		out+=Property+":";
		out+=QString::number(ap->KeyValues[i].Value);
		out+="},";
		out+=QString::number(ap->KeyValues[i].Time-ap->KeyValues[i-1].Time);
		out+=",";
		switch(ap->ease[i])
		{
		case Ease::ALLStart:
			out+="Ease.allStart";
			break;
		case Ease::AllEnd:
			out+="Ease.allEnd";
			break;
		case Ease::Linear:
			out+="Ease.linear";
			break;
		case Ease::QuadIn:
			out+="Ease.quadIn";
			break;
		case Ease::QuadOut:
			out+="Ease.quadOut";
			break;
		case Ease::QuadInOut:
			out+="Ease.quadInOut";
			break;
		case Ease::CubicIn:
			out+="Ease.cubicIn";
			break;
		case Ease::CubicOut:
			out+="Ease.cubicOut";
			break;
		case Ease::CubicInOut:
			out+="Ease.cubicInOut";
			break;
		case Ease::QuartIn:
			out+="Ease.quartIn";
			break;
		case Ease::QuartOut:
			out+="Ease.quartOut";
			break;
		case Ease::QuartInOut:
			out+="Ease.quartInOut";
			break;
		case Ease::QuintIn:
			out+="Ease.quintIn";
			break;
		case Ease::QuintOut:
			out+="Ease.quintOut";
			break;
		case Ease::QuintInOut:
			out+="Ease.quintInOut";
			break;
		case Ease::SineIn:
			out+="Ease.sineIn";
			break;
		case Ease::SineOut:
			out+="Ease.sineOut";
			break;
		case Ease::SineInOut:
			out+="Ease.sineInOut";
			break;
		case Ease::BackIn:
			out+="Ease.backIn";
			break;
		case Ease::BackOut:
			out+="Ease.backOut";
			break;
		case Ease::BackInOut:
			out+="Ease.backInOut";
			break;
		case Ease::CircIn:
			out+="Ease.circIn";
			break;
		case Ease::CircOut:
			out+="Ease.circOut";
			break;
		case Ease::CircInOut:
			out+="Ease.circInOut";
			break;
		case Ease::BounceIn:
			out+="Ease.bounceIn";
			break;
		case Ease::BounceOut:
			out+="Ease.bounceOut";
			break;
		case Ease::BounceInOut:
			out+="Ease.bounceInOut";
			break;
		case Ease::ElasticIn:
			out+="Ease.elasticIn";
			break;
		case Ease::ElasticOut:
			out+="Ease.elasticOut";
			break;
		case Ease::ElasticInOut:
			out+="Ease.elasticInOut";
			break;
		}
		out+=")";
	}
	out+=");\r\n";
}